package com.cddata;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.cddata.entity.Users;
import com.cddata.util.DBOperation;
import com.cddata.util.HibernateUtil;

@SuppressWarnings("serial")
public class Login extends JPanel implements ActionListener{

	public Login( ){
		setMaximumSize(new Dimension(DBOperation.getHelperBean().getMainWindowWidth(),DBOperation.getHelperBean().getMainWindowHieght()/4));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(4,3));
		JPanel infoPanel = new JPanel();
		JPanel namePanel = new JPanel();
		JPanel passwordPanel = new JPanel();
		JPanel buttonPanel = new JPanel();

		infoLabel = new JLabel();
		infoLabel.setForeground(Color.RED);

		usrName = new JTextField("kkm",15);
		password = new JPasswordField("kkm",15);
		JButton submit = new JButton("Submit");
		submit.addActionListener(this);

		infoPanel.add(infoLabel);
		namePanel.add(new JLabel("UserName :"));
		namePanel.add(usrName);
		passwordPanel.add(new JLabel("Password :"));
		passwordPanel.add(password);
		buttonPanel.add( submit );

		centerPanel.add(infoPanel);
		centerPanel.add(namePanel);
		centerPanel.add(passwordPanel);
		centerPanel.add(buttonPanel);

		add( centerPanel,BorderLayout.CENTER );
	}

	@SuppressWarnings("unchecked")
	public void actionPerformed(ActionEvent ae) {
		String action = ae.getActionCommand();
		Session session = HibernateUtil.getSessionFactory().openSession();;
		if( "submit".equalsIgnoreCase(action)) {
			String usrPassword = new String( password.getPassword() );
			String userName = usrName.getText();
			Criteria criteria = session.createCriteria(Users.class);
			criteria.add( Restrictions.eq("userName",userName));
			List result = criteria.list();
			if( result.size() == 0 ) {
				infoLabel.setText(" No User with this name " );
				infoLabel.setVisible(true);
			} else {
				for( Iterator<Users> it = result.iterator(); it.hasNext(); ) {
					Users user =  it.next();
					if( user.getUserName().equalsIgnoreCase(userName) && user.getPassword().equalsIgnoreCase(usrPassword)) {
						setVisible(false);
						Menu menu = new Menu();
						menu.setMaximumSize(new Dimension(80,500));
						DBOperation.populateContentTypes();
						DBOperation.getHelperBean().addContainer(menu);
						DBOperation.getHelperBean().addContainer(new AvailableCDs());
					} else {
						infoLabel.setText(" Password is incorrect " );
						infoLabel.setVisible(true);
					}
				}
			}
			session.close();
		}
	}

	private JTextField usrName = null;
	private JPasswordField password = null;
	private JLabel infoLabel = null;
}
